class Routine {
    // 显式声明属性
    public scopeUserInfo: string;
    constructor() {
        this.scopeUserInfo = 'scope.userInfo';
    }

    async getUserCode() {
        let isAuthBool = await this.isAuth(),
            code = '';
        if (isAuthBool)
            code = await this.getCode() as string;

        return code;
    }
    /**
     * 是否授权
     */
    isAuth() {
        let that = this
        return new Promise((resolve, reject) => {
            uni.getSetting({
                success(res) {
                    if (!res.authSetting[that.scopeUserInfo]) {
                        resolve(true)
                    } else {
                        resolve(true);
                    }
                },
                fail() {
                    resolve(false);
                }
            });
        });
    }
    async getCode() {
        let provider = await this.getProvider();
        return new Promise((resolve, reject) => {
            // if(Cache.has(STATE_R_KEY)){
            // 	return resolve(Cache.get(STATE_R_KEY));
            // }
            uni.login({
                provider: provider,
                success(res) {
                    // if (res.code) Cache.set(STATE_R_KEY, res.code, 10800);
                    return resolve(res.code);
                },
                fail() {
                    return reject(null);
                }
            })
        })
    }
    /**
     * 获取服务供应商
     */
    getProvider() {
        return new Promise((resolve, reject) => {
            uni.getProvider({
                service: 'oauth',
                success(res) {
                    resolve(res.provider);
                },
                fail() {
                    resolve(false);
                }
            });
        });
    }
}
export default new Routine();